﻿Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports ActiveDs

Public Class CheckPwd
    Public Shared Function Checkpwd(ByVal usersam As String)
        Dim acclock As String
        Dim phnum As String
        Dim mail As String
        Dim strNtName As String
        Dim objTrans As Object
        Dim strUserDn As String
        Dim strhomedrive As String
        Dim upn As String = ""
        Const adsNameInittypeGc As Integer = 3
        Const adsNameTypeNt4 As Integer = 3
        Const adsNameType1779 As Integer = 1
        Dim passwordExpirationDate As String
        Try
            strNtName = Replace(Replace(usersam, "WinNT://", ""), "/", "\")
            objTrans = CreateObject("NameTranslate")
            objTrans.Init(adsNameInittypeGc, "")
            objTrans.Set(adsNameTypeNt4, strNtName)
            strUserDn = objTrans.Get(adsNameType1779)
            Dim objUser As New DirectoryEntry("LDAP://" & strUserDn)
            mail = DirectCast(objUser.Properties("Mail").Value, String)
            upn = DirectCast(objUser.Properties("userPrincipalName").Value, String)
            phnum = DirectCast(objUser.Properties("telephoneNumber").Value, String)
            Dim ctx As New PrincipalContext(ContextType.Domain)
            Dim user As UserPrincipal
            user = UserPrincipal.FindByIdentity(ctx, DirectCast(objUser.Properties("userPrincipalName").Value, String))
            strhomedrive = user.HomeDirectory
            Dim dirEntryResults As New DirectoryEntry("WinNT://" & Replace(usersam, "\", "/"))
            Dim native As IADsUser = dirEntryResults.NativeObject
            If user.PasswordNeverExpires Then
                passwordExpirationDate = "Password does not Expire"
            Else
                passwordExpirationDate = native.PasswordExpirationDate.ToString()
            End If
            If user.AccountLockoutTime Is Nothing Then
                acclock = "Not Locked out"
            Else
                acclock = "Locked out"
            End If
            If upn = Nothing Then upn = "No UPN Listed in AD"
            If phnum = Nothing Then phnum = "No Number Listed in AD"
            If mail = Nothing Then mail = "No Email address listed in AD"
        Catch ex As Exception
            acclock = "No Access to Domain Controller"
            phnum = "No Access to Domain Controller"
            mail = "No Access to Domain Controller"
            passwordExpirationDate = "No Access to Domain Controller"
        End Try
        Return passwordExpirationDate & "|" & acclock & "|" & phnum & "|" & mail & "|" & upn & "|" & strhomedrive
    End Function
End Class
